package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.GetAddressResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.MyHarvestAddressProtocol

class MyHarvestAddressPresenter(context: Context, mView: MyHarvestAddressProtocol.IView): BasePresenter(context), MyHarvestAddressProtocol.IPresenter {
    private var mView: MyHarvestAddressProtocol.IView? = mView
    private var getUserAddressListIsBusying = false

    override fun getUserAddressList() {
        if (getUserAddressListIsBusying) return
        getUserAddressListIsBusying = true
        OperationManager.getInstancce().getAddress(object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                getUserAddressListIsBusying = false
                if (state == 200) {
                    val data = GsonBuilder().create().fromJson<ArrayList<GetAddressResponse>>(
                        GsonBuilder().create().toJson(obj), object : TypeToken<ArrayList<GetAddressResponse>>(){}.type)
                    mView?.getUserAddressListSuccessed(data)
                }
            }

            override fun onFail(code: Int, message: String) {
                getUserAddressListIsBusying = false
                mView?.getUserAddressListFailed(code, message)
            }

        })
    }

}